package cn.tedu.boot.ers.controller;

import cn.tedu.boot.ers.exception.ServiceException;
import cn.tedu.boot.ers.pojo.dto.*;
import cn.tedu.boot.ers.pojo.vo.AdminSimpleVO;
import cn.tedu.boot.ers.pojo.vo.UserDetailsVO;
import cn.tedu.boot.ers.service.IUserService;
import cn.tedu.boot.ers.web.JsonResult;
import cn.tedu.boot.ers.web.State;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping (value = "/users" ,produces = "application/json;charset=utf-8")
public class UserController {
    @Autowired
    IUserService service;

    @ApiOperation("用户注册")
    @PostMapping("/reg")
    public JsonResult<Void> reg(UserDTO userDTO){

        service.addNew(userDTO);
        return JsonResult.ok();
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public JsonResult<UserDetailsVO> login(UserLoginDTO userloginDTO, HttpSession session){
        UserDetailsVO result = service.UserLogin(userloginDTO, session);
        return JsonResult.ok(result);
    }

    @PostMapping("/update/password")
    @ApiOperation("修改密码")
    public JsonResult<Void> UpdatePassword(UserUpdatePasswordDTO updatePasswordDTO){
        service.updatePassword(updatePasswordDTO);
        return JsonResult.ok();
    }

    @PostMapping("/update/info")
    @ApiOperation("修改基本信息")
    public JsonResult<Void> setUserInfo(UserUpdateInfoDTO userUpdateInfoDTO){
        service.updateInfo(userUpdateInfoDTO);
        return JsonResult.ok();
    }

    @PostMapping("/update/money")
    @ApiOperation("修改用户余额")
    public JsonResult<Void> setUserBalance(UserUpdateMoneyDTO userUpdateMoneyDTO){
        service.updateBalance(userUpdateMoneyDTO);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("获取所有用户信息")
    public JsonResult<List<UserDetailsVO>> allUser(){
        List<UserDetailsVO> list = service.userList();
        return JsonResult.ok(list);
    }

    @ApiOperation("客户端获取当前会话中的对象")
    @GetMapping("/current/user")
    public JsonResult<UserDetailsVO> currentUser(HttpSession session) {
        //把登录成功时保存的用户对象取出 返回给客户端
        Object user = session.getAttribute("user");
        if (user == null){
            throw new ServiceException(State.ERR,"当前没有登录对象");
        }
        System.out.println("user = " + user);
        return JsonResult.ok((UserDetailsVO) session.getAttribute("user"));
    }

    @ApiOperation("客户端清空当前会话中的对象/登出")
    @PostMapping("/logout")
    public JsonResult<Void> logout(HttpSession session) {
        Object admin = session.getAttribute("user");
        if (admin == null){
            throw new ServiceException(State.ERR,"当前没有登录对象");
        }
        log.warn("即将退出当前user");
        //把登录成功时保存的用户对象删除
        session.removeAttribute("user");
        return JsonResult.ok();
    }
}
